package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.db.api.FreezeApi;
import com.tanhua.dubbo.db.api.UserInfoApi;
import com.tanhua.dubbo.mongo.api.CommentApi;
import com.tanhua.model.db.Freeze;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CommentsService {


    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private FreezeApi freezeApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //分页查询评论列表
    public PageResult findAllComments(String movementId, Integer page, Integer pagesize) {

        //根据movementId获得评论list
        int commentType = 2;

        List<Comment> comments = commentApi.findAllComments(movementId, page, pagesize, commentType);

        //判断comments是否为空,
        if (comments == null || comments.size() <= 0) {

            return new PageResult();
        }

        List<Long> ids = CollUtil.getFieldValues(comments, "userId", Long.class);
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(ids);

        //创建 CommentVo的list集合
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : comments) {

            UserInfo userInfo = infoMap.get(comment.getUserId());
            //封装
            CommentVo vo = CommentVo.init(userInfo, comment);

            vos.add(vo);
        }

        return new PageResult(page, pagesize, 0l, vos);
    }

    //评论-提交
    public void saveComment(String movementId, String comment) {
        //1.获得登录中的userId
        Long userId = UserHolder.getUserId();


        //查询该用户否被冻结
        String key = Constants.USER_FREEZE+userId;
        Freeze freeze = freezeApi.findFreezeByUserId(userId);
        if (freeze != null) {
            UserInfo userInfo = userInfoApi.findById(userId);
            //获取从redis中获取的数据
            String freezeTime = redisTemplate.opsForValue().get(key);
            Integer freezingTime = freeze.getFreezingTime();
            if (freezeTime == null && freezingTime != 3) {
                userInfo.setUserStatus("1");
                userInfoApi.updateUserInfo(userInfo);
            }
            if (freezeTime != null) {
                if (userInfo.getUserStatus().equals("2") && freeze.getFreezingRange() == 2) {
                    throw new BusinessException(ErrorResult.freezeSpeakError());
                }
            }
        }

        int commentType = 2;

        //创建Comment对象
        Comment comment1 = new Comment();
        //传入数据
        comment1.setPublishId(new ObjectId(movementId));
        comment1.setCommentType(commentType);
        comment1.setContent(comment);
        comment1.setUserId(userId);
        comment1.setCreated(System.currentTimeMillis());
        //调用方法保存,返回该动态的总评论数
        Integer count = commentApi.saveComment(comment1);
        System.out.println(comment);


    }


}